package com.demo.swingquickticket.service;

import com.alibaba.fastjson.JSONObject;
import com.demo.swingquickticket.entity.Account;
import com.demo.swingquickticket.entity.JsonResult;
import com.demo.swingquickticket.util.ImageUtil;
import com.demo.swingquickticket.util.http.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class AccountService {

    public String getLoginPic() {
        RequestEntity requestEntity = new RequestEntity("https://kyfw.12306.cn/passport/web/create-qr64", HttpContents.POST_TYPE);
        requestEntity.addParam("appid", "otn");
        requestEntity.setHttpResult(new QrCode());
        requestEntity.buildHttpClient();
        ResponseEntity responseEntity = HttpClientUtils.doRequest(requestEntity);
        if (responseEntity.getHttpCode().equals("-1")) {
            return null;
        }
        JSONObject qrJsonStr = JSONObject.parseObject(responseEntity.getResult());
        String qrStr = qrJsonStr.getString("image");
        ImageUtil.base64ToImage(qrStr);
        return qrJsonStr.getString("uuid");
    }

    public JsonResult picLogin(String uuid) {
        Account account = DataService.getAccount();
        RequestEntity requestEntity = new RequestEntity("https://kyfw.12306.cn/passport/web/checkqr", HttpContents.POST_TYPE);
        requestEntity.setCookieList(account.getLoginCookie());
        requestEntity.addParam("appid", "otn");
        requestEntity.addParam("uuid", uuid);
        requestEntity.setHttpResult(new QrCheck());
        requestEntity.buildHttpClient();
        ResponseEntity responseEntity = HttpClientUtils.doRequest(requestEntity);
        if ("200".equals(responseEntity.getHttpCode())) {
            return new JsonResult("登入成功！");
        } else {
            return new JsonResult(JsonResult.ERROR_CODE, responseEntity.getMessage());
        }
    }

    public String checkLogin() {
        Account account = DataService.getAccount();
        RequestEntity requestEntity = new RequestEntity("https://kyfw.12306.cn/otn/login/checkUser", HttpContents.GET_TYPE);
        requestEntity.addHeader("Origin", "https://kyfw.12306.cn");
        requestEntity.addHeader("X-Requested-With", "XMLHttpRequest");
        requestEntity.addHeader("Content-Type", "application/x-www-form-urlencoded");
        requestEntity.setCookieList(account.getLoginCookie());
        requestEntity.setHttpResult(new CheckLogin());
        requestEntity.buildHttpClient();
        ResponseEntity responseEntity = HttpClientUtils.doRequest(requestEntity);
        System.out.println(JSONObject.toJSONString(responseEntity));
        return JSONObject.toJSONString(responseEntity);
    }

    private class QrCode implements HttpResult {
        @Override
        public ResponseEntity doResult(RequestEntity request, ResponseEntity response) {
            Account account = DataService.getAccount();
            account.setLoginCookie(request.getCookieStore().getCookies());
            return response;
        }
    }

    private class QrCheck implements HttpResult {
        @Override
        public ResponseEntity doResult(RequestEntity request, ResponseEntity response) {
            if (response.getHttpCode().equals("200")) {
                JSONObject qrCheckResultJsonStr = JSONObject.parseObject(response.getResult());
                switch (qrCheckResultJsonStr.getString("result_code")) {
                    case "0": {
                        response.setHttpCode("400");
                        response.setMessage("还未扫码认证");
                        System.out.println("还未扫码认证");
                        break;
                    }
                    case "1": {
                        response.setHttpCode("400");
                        response.setMessage("已扫码未确认");
                        System.out.println("已扫码未确认");
                        break;
                    }
                    case "2": {
                        request.setUrl("https://kyfw.12306.cn/passport/web/auth/uamtk");
                        request.removeParam("uuid");
                        request.setHttpResult(new UMarkCheck());
                        System.out.println("扫码认证成功");
                        return HttpClientUtils.doRequest(request);
                    }
                    case "3": {
                        response.setHttpCode("400");
                        response.setMessage("验证码过期");
                        System.out.println("验证码过期");
                        break;
                    }
                }
            }
            return response;
        }
    }

    private class UMarkCheck implements HttpResult {
        @Override
        public ResponseEntity doResult(RequestEntity request, ResponseEntity response) {
            if ("200".equals(response.getHttpCode())) {
                JSONObject uMarkResultJsonStr = JSONObject.parseObject(response.getResult());
                String newapptk = uMarkResultJsonStr.getString("newapptk");
                if (!StringUtils.isEmpty(newapptk)) {
                    request.setUrl("https://kyfw.12306.cn/otn/uamauthclient");
                    request.removeParam("appid");
                    request.addParam("tk", newapptk);
                    request.setHttpResult(new UamAuth());
                    System.out.println("获取newapptk成功" + newapptk);
                    return HttpClientUtils.doRequest(request);
                } else {
                    response.setHttpCode("400");
                    response.setMessage("获取newapptk失败");
                    System.out.println("获取newapptk失败");
                }
            }
            return response;
        }
    }

    private class UamAuth implements HttpResult {

        @Override
        public ResponseEntity doResult(RequestEntity request, ResponseEntity response) {
            if ("200".equals(response.getHttpCode())) {
                DataService.getAccount().setLoginCookie(request.getCookieStore().getCookies());
            } else {
                response.setHttpCode("400");
                response.setMessage("UamAuth认证失败");
                System.out.println("UamAuth认证失败");
            }
            return response;
        }
    }

    private class CheckLogin implements HttpResult {
        @Override
        public ResponseEntity doResult(RequestEntity request, ResponseEntity response) {
            return response;
        }
    }
}
